<?php
/**
 * 
 * 系统用户管理
 */
class SystemController extends Cola_Controller {
	
	/**
	 * 默认加载
	 * @see Cola_Controller::init()
	 */
	public function init(){
		$this->model = $this->model('System');
		$this->loginUserInfo = $this->model->getLoginInfo();
		if( $this->loginUserInfo ){
			$this->view->loginUserInfo = $this->loginUserInfo;
		}else{
			echo '<script>';
			echo 'alert("登录超时，请重新登录");';
			echo 'window.location.href="http://admin.wowlongteng.com"';
			echo '</script>';
			exit;
		}
	}
	
	/**
	 * 
	 * 列表页
	 */
    public function indexAction() {
    	$numPerPage = $this->post('numPerPage',20);
    	$currentPage = $this->post('pageNum',1);
    	$totalCount = $this->model->getList(true);
		$list = $this->model->getList(false, $currentPage, $numPerPage);
        $this->view->currentPage = $currentPage; //第几页
        $this->view->numPerPage = $numPerPage;   //每页显示多少条
        $this->view->totalCount = $totalCount;   //总共有多少条
        $this->view->list = $list;
        $this->display();
    }
    
    /**
     * 
     * 添加用户
     */
    public function addAction() {
    	if( Cola_Request::isPost() ){
	        $data = array('user_type' => intval($this->post('user_type')), 
				          'username' => trim($this->post('username')), 
				          'password' => MD5(MD5(trim($this->post('password')))), 
				          'email' => trim($this->post('email')), 
				          'status' => intval($this->post('status')), 
				          'addtime' => time());
			$result = $this->model->add( $data );

			if( $result ){
			    $jsonData = array('statusCode' => 200, 'message' => '添加成功');
			}else{
			    $jsonData = array('statusCode' => 300, 'message' => '添加失败');
			}   
			echo json_encode($jsonData); 
			exit;
    	}
        $this->display();
    }
    
    /**
     * 
     * 编辑用户
     */
    public function editAction() {
        $id = intval($this->get('id'));
		$userInfo = $this->model->getInfoById( $id );
        $this->view->userInfo = $userInfo;
        
        //判断用户类型
        if( Cola_Request::isPost() && $id > 0 ){
	        $data = array('user_type' => intval($this->post('user_type')), 
				          'username' => trim($this->post('username')), 
				          'email' => trim($this->post('email')), 
				          'status' => intval($this->post('status')));
	        trim($this->post('password')) != '' && $data['password'] = MD5(MD5(trim($this->post('password'))));
	        
	        $checkExist = $this->model->checkExist( trim($this->post('username')), $id);
    		if( $checkExist == false ){
	        	$result = $this->model->edit($id, $data);
    		}else{
    			$result = false;
    		}
			if( $result ){
			    $jsonData = array('statusCode' => 200, 'message' => '修改信息成功');
			}else{
			    $jsonData = array('statusCode' => 300, 'message' => '修改信息失败');
			}
			echo json_encode($jsonData);
			exit;
    	}
        
        $this->display();
    }
    
    /**
     * 
     * 修改用户密码
     */
    public function editpasswordAction() {
        $id = intval($this->get('id'));
		$userInfo = $this->model->getInfoById( $id );
        $this->view->userInfo = $userInfo;
        if( $this->loginUserInfo['uid'] != $id ){
        	echo 'System Error';exit;
        }
        
        //判断用户类型
        if( Cola_Request::isPost() && $id > 0 ){
	        $data = array('password' => MD5(MD5(trim($this->post('password')))));
        	$checkExist = $this->model->checkExist( trim($this->post('username')), $id);
    		if( $checkExist == false ){	        
	        	$result = $this->model->edit($id, $data);
    		}else{
    			$result = false;
    		}
			if( $result ){
			    $jsonData = array('statusCode' => 200, 'message' => '修改密码成功');
			}else{
			    $jsonData = array('statusCode' => 300, 'message' => '修改密码失败');
			}
			echo json_encode($jsonData);
			exit;
    	}
        
        $this->display();
    }
    
    /**
     * 
     * 删除用户
     */
    public function delAction(){
        $id = intval($this->get('id'));
        //判断用户类型
		$result = $this->model->del($id);
		if( $result ){
		    $jsonData = array('statusCode' => 200, 'message' => '删除成功');
		}else{
		    $jsonData = array('statusCode' => 300, 'message' => '删除失败');
		}
		echo json_encode($jsonData);
    }
}
?>
